﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace T2_garage
{
    public partial class MainScreen : Form
    {
        FileIO file;
        Communication comm;

        public MainScreen()
        {
            InitializeComponent();
            file = new FileIO();
            comm = new Communication();


            if (Vm110.OpenDevice(0) < 0)
            {
                MessageBox.Show("Er is geen verbinding met de Vm110.");
            }
            else
            {
                Vm110.ClearAllDigital();
            }

            bgwStatusChecker.RunWorkerAsync();
        }

        private void MainScreen_Load(object sender, EventArgs e)
        {
        }

        private void btnPark_Click(object sender, EventArgs e)
        {
            if (comm.checkEntrance())
            {
                int index = file.writeValue();
                if (index != -1)
                {

                    tbAuthCodePark.Text = file.authCodes[index].ToString();
                    int place = index + 1;
                    lblPlacePark.Text = place.ToString();
                    comm.parkSpot(place);

                }
            }
            else
            {
                MessageBox.Show("Er staat geen auto op de ingang.");
            }
        }

        
        private void btnExit_Click(object sender, EventArgs e)
        {
            file.writeFile();
            bgwStatusChecker.Dispose();
            this.Close();
            Application.Exit();
        }
         

        private void btnFetch_Click(object sender, EventArgs e)
        {
            try
            {
                if (comm.checkExit())
                {
                    int authCode = Int32.Parse(tbAuthCodeFetch.Text);
                    if (authCode > 0)
                    {
                        int place = file.deleteValue(authCode);
                        if (place > 0)
                        {
                            MessageBox.Show("Uw auto wordt opgehaald.");
                            comm.fetchSpot(place);
                        }
                        else
                        {
                            MessageBox.Show("Ongeldige authenticatie code.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ongeldige authenticatie code.");
                    }
                }
                else
                {
                    MessageBox.Show("Er staat al een auto op de uitgang.");
                }
            }
            catch (FormatException x)
            {
                MessageBox.Show("Voer astublieft een geldige authenticatie code in.");
                x.ToString();
            }

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            int i = checkStatus();
            if (!this.IsHandleCreated || this.IsDisposed) return;
            try
            {
                Invoke((MethodInvoker)delegate
                {
                    tsTijd.Text = "Tijd: " + DateTime.Now.ToString("HH:mm:ss");

                    switch (i)
                    {
                        case 0:
                            tsStatus.ForeColor = Color.Green;
                            tsStatus.Text = "Klaar";
                            btnPark.Enabled = true;
                            btnFetch.Enabled = true;
                            btnExit.Enabled = true;
                            break;
                        case 1:
                            tsStatus.ForeColor = Color.Orange;
                            tsStatus.Text = "Wacht";
                            btnPark.Enabled = false;
                            btnFetch.Enabled = false;
                            btnExit.Enabled = false;
                            break;
                        case 2:
                            tsStatus.ForeColor = Color.Orange;
                            tsStatus.Text = "Parkeren";
                            btnPark.Enabled = false;
                            btnFetch.Enabled = false;
                            btnExit.Enabled = false;
                            break;
                        case 3:
                            Vm110.ClearDigitalChannel(1);
                            Vm110.ClearDigitalChannel(2);
                            Vm110.ClearDigitalChannel(3);
                            Vm110.ClearDigitalChannel(4);
                            Vm110.ClearDigitalChannel(5);
                            tsStatus.ForeColor = Color.Orange;
                            tsStatus.Text = "Wacht";
                            break;
                        case 4:
                            tsStatus.ForeColor = Color.Orange;
                            tsStatus.Text = "Ophalen";
                            btnPark.Enabled = false;
                            btnFetch.Enabled = false;
                            btnExit.Enabled = false;
                            break;
                        default:
                            tsStatus.ForeColor = Color.Red;
                            tsStatus.Text = "Error!";
                            btnPark.Enabled = false;
                            btnFetch.Enabled = false;
                            btnExit.Enabled = false;
                            break;
                    }
                });
            }
            catch
            {

            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bgwStatusChecker.RunWorkerAsync();
        }

        public int checkStatus()
        {
            bool bit1 = !(Vm110.ReadDigitalChannel(1));
            bool bit2 = !(Vm110.ReadDigitalChannel(2));
            bool bit3 = !(Vm110.ReadDigitalChannel(3));
            bool bit4 = !(Vm110.ReadDigitalChannel(4));
            bool bit5 = !(Vm110.ReadDigitalChannel(5));

            if (!bit5 && !bit4 && !bit3)
            {
                return 0;
            }
            else if (bit5 && !bit4 && !bit3)
            {
                return 1;
            }
            else if (!bit5 && !bit4 && bit3)
            {
                return 2;
            }
            else if (!bit5 && bit4 && bit3)
            {
                return 3;
            }
            else if (!bit5 && bit4 && !bit3)
            {
                return 4;
            }
            else
            {
                return -1;
            }
        }

        private void MainScreen_FormClosed(object sender, FormClosedEventArgs e)
        {
            bgwStatusChecker.Dispose();
            Application.Exit();
        }
    }
}
